﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord;
using System.Collections;
using System.Reflection;

namespace Windvoice
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //Blog Route
            routes.MapRoute(
                "Blog", // Route name
                "Blogs", // URL with parameters
                new { controller = "Blog", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            routes.MapRoute(
                "BlogList", // Route name
                "Blogs/{key}", // URL with parameters
                new { controller = "Blog", action = "Index", key = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            routes.MapRoute(
                "BlogDetail", // Route name
                "Blog/{key}", // URL with parameters
                new { controller = "Blog", action = "Detail", key = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            //Album Route
            routes.MapRoute(
                "Album", // Route name
                "Album", // URL with parameters
                new { controller = "Album", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            routes.MapRoute(
                "AlbumDetail", // Route name
                "Album/{id}", // URL with parameters
                new { controller = "Album", action = "Detail", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            routes.MapRoute(
                "PhotoDetail", // Route name
                "Album/Photo/{id}", // URL with parameters
                new { controller = "Album", action = "Photo", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            //Resume
            //routes.MapRoute(
            //    "Resume", // Route name
            //    "Experiences", // URL with parameters
            //    new { controller = "Home", action = "Resume" }, // Parameter defaults
            //    new string[] { "Windvoice.Controllers" }
            //);
            routes.MapRoute(
                "Links", // Route name
                "Links", // URL with parameters
                new { controller = "Links", action = "Index" }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );
            routes.MapRoute(
                "About", // Route name
                "About", // URL with parameters
                new { controller = "About", action = "Index" }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            routes.MapRoute(
                "404", // Route name
                "404", // URL with parameters
                new { controller = "Home", action = "_404" }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );
            routes.MapRoute(
                "500", // Route name
                "500", // URL with parameters
                new { controller = "Home", action = "_500" }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Windvoice.Controllers" }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("~/Config/activerecord.xml"));
            var Assemblies = new Assembly[] { System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Blog.Category)), System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Albums.Album)), System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Authorize.User)) };
            ActiveRecordStarter.Initialize(Assemblies, source);
            
            log4net.Config.XmlConfigurator.Configure();

            //enable mono wcf
            if (Environment.GetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT") != "yes")
            {
                Environment.SetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT", "yes");
            }
        }
    }
}